/**
 * Copyright 2013 Matt Holland <code.monkey.ota at gmail.com>
 *
 * This file is part of BotTender.
 *
 * BotTender is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * BotTender is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with BotTender. If not, see <http://www.gnu.org/licenses/>.
 */


package ota.ircbot.bottender;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.pircbotx.Channel;
import org.pircbotx.PircBotX;
import org.pircbotx.User;
import org.pircbotx.hooks.ListenerAdapter;
import org.pircbotx.hooks.events.MessageEvent;

public class ServeDrink extends ListenerAdapter {
	
	//private BotTender tender;
	private PircBotX bot;
	
	public ServeDrink(PircBotX mnBt) {
		bot = mnBt;
	}
	
	public void onMessage(MessageEvent event) {
		if(event.getMessage().matches("^(?i:"+bot.getNick()+")[,:].*")) {
			event.respond("One " +extractDrink(event.getMessage())+ " coming up...");
			serve(extractDrink(event.getMessage()), event.getUser(), event.getChannel());
		}
	}
	
	private String extractDrink(String message) {
		
		List<String> toBeDel = Arrays.asList("a", "an", "some", "of", "glass", "shot", "pint");
		
		// Remove any words on the list, first convert message to lower case
		String result = message.toLowerCase();
		result = result.replaceAll("^"+bot.getNick().toLowerCase()+"[,:]\\S*", ""); // Remove the address to the bot
		Iterator<String> it = toBeDel.iterator();
		while(it.hasNext()) { // Cycle through the list, remove any words on it followed by space
			result = result.replaceAll(it.next()+ " ", "");
		}		
						
		result = result.replaceAll(",.*$", ""); // Remove commas and everything after them
		result = result.trim(); // remove any leading or trailing whitespace
				
		return result; // Return it, this can be anything, that isn't the bot's name, 
		// a word on the list, or after a comma.
	}
	private void serve(String drink, User who, Channel where) {
		
		bot.sendAction(where, "pours a " +drink+ " and sets it on the bar for " +who.getNick()+ ".");
	}

}
